package com.waidi.clothespad.common;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Db extends SQLiteOpenHelper {

    //类没有实例化,是不能用作父类构造器的参数,必须声明为静态

    private static final String name = "tps10"; //数据库名称

    private static final int version = 1; //数据库版本

    public Db(Context context) {

        //第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类

        super(context, name, null, version);

    }
    //调用父类构造器
    public Db(Context context, String name, CursorFactory factory,
                          int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
         //创建数据表-员工考勤数据信息表
        db.execSQL("CREATE TABLE IF NOT EXISTS attend (zggh int,cardid varchar(24),name varchar(255),time DATETIME primary key,type int)");
        //创建数据表-吊挂数据信息表
        db.execSQL("CREATE TABLE IF NOT EXISTS Production_record(id int primary key,ptime DATETIME,orderid varchar(64),styleid varchar(64),style_size varchar(64), style_color varchar(64),gx_code varchar(6),worker_name varchar(6));");

        db.execSQL("CREATE TABLE IF NOT EXISTS xxxx (id int primary key,info varchar(64) );");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

       /* db.execSQL("ALTER TABLE person ADD phone VARCHAR(12)"); //往表中增加一列*/

    }

    public void test()
    {

        SQLiteDatabase db = this.getWritableDatabase();
/*        //使用execSQL方法向表中插入数据
        db.execSQL("insert into xxxx(info) values(\"ppp\")");
        //使用insert方法向表中插入数据*/

 /*       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        String tt = format.format(new Date()); //产生该条生产记录的时间
        ContentValues values = new ContentValues();
        values.put("info",tt );
        db.insert("xxxx", "id", values);
        //values.clear();

*/      ContentValues values = new ContentValues();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        String ptime = format.format(new Date()); //产生该条生产记录的时间
        values.putNull("id");
        values.put("ptime", ptime);
        values.put("orderid", "pp");
        values.put("styleid", "1");
        values.put("style_size", "xxl");
        values.put("style_color", "red");
        values.put("gx_code", "t56");
        values.put("worker_name", "xiaohong");
        db.insert("Production_record", "ptime", values);
        values.clear();



        StringBuilder sql=new StringBuilder("SELECT * FROM Production_record");
        Cursor cursor = db.rawQuery(sql.toString(), null);

        while (cursor.moveToNext()) {

            int id = cursor.getInt(0); //获取第一列的值,第一列的索引从0开始
            String info = cursor.getString(1);//获取第二列的值
            Log.i("DB",id+"   "+info);
            Log.i("DB",Integer.toString(cursor.getCount())+"  "+Integer.toString(cursor.getPosition()));

        }
        cursor.close();
        db.close();
    }
}